package com.fjut.zr.judge_system.VJ;

import com.fjut.zr.judge_system.POJO.PO.TVjStatusPO;
import com.fjut.zr.judge_system.POJO.PO.TVjSubmitPO;
import com.fjut.zr.judge_system.VJ.Submitter.VJSubmitter;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Vjudge {
    List<VjSubmitter> listVjSubmitters=new ArrayList<VjSubmitter>();//创建测评机组
    List<BlockingQueue<TVjSubmitPO>> queue = new ArrayList<BlockingQueue<TVjSubmitPO>>();//创建阻塞队列组
    public Vjudge()//初始化
    {
        TVjStatusPO vjStatusPO=new TVjStatusPO();
        String ojkeys[] = VJSubmitter.vj_keys;
        for(int j = 0;j < ojkeys.length; j++){
            vjStatusPO.setVjId(j);
            vjStatusPO.setOjName(ojkeys[j]);
            vjStatusPO.setVjQueue(0);
            vjStatusPO.setVjStatus("创建成功");
            //更新测评机状态
            listVjSubmitters.add(new VjSubmitter(j, j * 10 + j,this));
            queue.add(new LinkedBlockingQueue<TVjSubmitPO>());
        }
        DO();
    }

    public int addSubmit(TVjSubmitPO info,int oj){
        try {
            queue.get(oj).put(info);
        } catch (InterruptedException e) {
            // Tool.log(e);
        }
        return 1;
    }
    private void DO(){
        for (VjSubmitter vjSubmitter : listVjSubmitters) {
            Thread t1 = new Thread(vjSubmitter);
            vjSubmitter.setSelfThread(t1);//传线程给测评机以便读取线程id
            t1.start();
        }
    }

    }

